package cn.hnucm.searchesservice.controller;


import cn.hnucm.searchesservice.entity.TbGoodsImage;
import cn.hnucm.searchesservice.service.TbGoodsImageService;
import cn.hnucm.searchesservice.utils.MyLog;
import cn.hnucm.searchesservice.utils.Result;
import cn.hnucm.searchesservice.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品图片表 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-20
 */
@Api(tags = "商品图片表")
@RestController
@CrossOrigin
@RequestMapping("/serchesservice/tbGoodsImage")
public class TbGoodsImageController {

    @Autowired
    @Qualifier("tbGoodsImageServiceImpl")
    private TbGoodsImageService service;

    /**
     * 获取一个商品号的所有图片路径
     * @param goodsId
     * @return
     */
    @ApiOperation("获取一个商品号的所有图片路径")
    @MyLog("获取一个商品号的所有图片路径")
    @GetMapping("/getImageByGoodsId/{goodsId}")
    @ResponseBody
    public Result getImageByGoodsId(@PathVariable("goodsId") Integer goodsId){
        if (goodsId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }

        List<TbGoodsImage> goodsImages = service.getImageByGoodsId(goodsId);
        if (goodsImages == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goodsImages.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goodsImages",goodsImages);
    }

    /**
     * 删除一条记录
     * @param tbGoodsImage
     * @return
     */
    @ApiOperation("删除一条记录")
    @MyLog("删除一条记录")
    @PostMapping("/deleteGoodsImage")
    @ResponseBody
    public Result deleteGoodsImage(@RequestBody TbGoodsImage tbGoodsImage){
        if (tbGoodsImage == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.deleteGoodsImage(tbGoodsImage);
        if (aBoolean == null || !aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 添加一条图片记录
     * @param tbGoodsImage
     * @return
     */
    @ApiOperation("添加一条图片记录")
    @MyLog("添加一条图片记录")
    @PostMapping("/addGoodsImage")
    @ResponseBody
    public Result addGoodsImage(@RequestBody TbGoodsImage tbGoodsImage){
        if (tbGoodsImage == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.addGoodsImage(tbGoodsImage);
        if (aBoolean == null || !aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 修改一条图片记录
     * @param goodsId
     * @param oldImagePath
     * @param newImagePath
     * @return
     */
    @ApiOperation("修改一条图片记录")
    @MyLog("修改一条图片记录")
    @GetMapping("/updateGoodsImage/{goodsId}/{oldImagePath}/{newImagePath}")
    @ResponseBody
    public Result updateGoodsImage(@PathVariable("goodsId") Integer goodsId,
                                   @PathVariable("oldImagePath") String oldImagePath,
                                   @PathVariable("newImagePath") String newImagePath){
        if (goodsId == null || oldImagePath == null || newImagePath == null){
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.updateGoodsImage(goodsId, oldImagePath, newImagePath);
        if (aBoolean == null || !aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }



}

